<?php
namespace Android\Controller;


class StudentController extends BaseController {

    /**
     * 查看指定学生的考勤记录
     */
    public function getStudentAttend() {

        $student_id = I ('student_id','');
        if (empty($student_id))
            $this->out($this->fail,'Empty student_id');

        $student = D ('Student')->getStudentById($student_id,'id,student_id,name,sex,telphone,photo');
        if (empty($student))
            $this->out($this->fail, '学生不存在');

        $value['student'] = $student;

        //$attendArr = D ('Attend')->getAttendBySid($student['id'],$this->term['id']);

        // 获取学生考勤次数统计信息
        $Attend = D ('Attend');
        $map['student_id'] = $student['id'];
        $map['term_id'] = $this->term['id'];

        // 旷课次数 status = 1
        $map['status'] = 1;
        $kkCount = $Attend->getCountByWhere($map);
        // 迟到次数
        $map['status'] = 2;
        $cdCount = $Attend->getCountByWhere($map);
        // 请假次数
        $map['status'] = 3;
        $qjCount = $Attend->getCountByWhere($map);



        // 处理考勤信息 教师名称 课程名称
//        $Teacher = D ('Teacher');
//        $Course = D ('Course');
//        foreach ($attendArr as $k=>$v) {
//            $attendArr[$k]['teacher_name'] = $Teacher->getTeacherName($v['teacher_id']);
//            $attendArr[$k]['course_name'] = $Course->getCourseName($v['course_id']);
//        }

        $value['attend'] = array(
            'kk_count' => $kkCount,
            'cd_count' => $cdCount,
            'qj_count' => $qjCount,
        );
        $this->out($this->success,'Success', $value);
    }


    /**
     * 获取学生详细的出勤记录
     */
    public function getStudentDetailAttend() {

        $student_id = I ('request.student_id',null);
        $status = I ('request.status',null);

        if (empty($student_id) || empty($status))
            $this->out($this->fail,'Empty parameter');

        $attendArr = D ('Attend')->getSpecificAttend($student_id,$status,$this->term['id']);
        if (empty($attendArr))
            $this->out($this->fail,'没有相关考勤记录');

        // 处理考勤信息 教师名称 课程名称
        $Teacher = D ('Teacher');
        $Course = D ('Course');
        foreach ($attendArr as $k=>$v) {
            $attendArr[$k]['teacher_name'] = $Teacher->getTeacherName($v['teacher_id']);
            $attendArr[$k]['course_name'] = $Course->getCourseName($v['course_id']);
        }

        $this->out($this->success,'Success',$attendArr);
    }


    /**
     * 测试页面
     */
    public function test() {
        $this->display();
    }


    /**
     * 上传头像接口
     */
    public function uploadPhoto() {

        $data['id'] = I ('request.student_id',null);
        if (empty($data['id']) || empty($_FILES['file']['name']))
            $this->out($this->fail, 'Empty parameter');

        // 上传头像
        $Upload = new \Api\Libs\Upload();
        $res = $Upload->upload('img',true,false,array('width'=>250,'height'=>300));

        if ($res['status'] !== 1) {
            $this->out($this->fail,$res['msg']);
        }

        // 保存学生信息
        $data['photo'] = $this->url.$res['path'];
        // 头像初始化状态 1 上传未审核
        $data['photo_status'] = 1;
        $Student = M ('Student');
        $flag = $Student->save($data);

        if ($flag === false)
            $this->out($this->fail,'上传头像失败');

        $this->out($this->success,$res['msg'],$data['photo']);
        //$res = $Upload->upload('img',true,false,array('width'=>200,'height'=>160));
    }


}